﻿using System;
using System.Collections.Generic;

using System.Text;
using Model;
using System.Collections;
using Components;
using System.Data;
using UI.Lib.Factory;

namespace UI.Lib
{
    /// <summary>
    /// 当前用户集合
    /// </summary>
    public class Context
    {
        private static Hashtable _keys; //用户的登录标识
        private static Hashtable _users; //部门 公司 登录者信息
        private static Hashtable _names; //当前用户的用户名数组
        private static Hashtable _loginGuid;
        private static Hashtable _cache; //用于存放私人的临时变量

        private static Hashtable _globlos;

        //保存全局变量
        public static void AddGloblos(Hashtable tb)
        {
            _globlos = new Hashtable();

            _globlos.Add(ProcessHelper.Entity.GetPid(), tb);
        }

        public static string Global(string key)
        {
            string pid = ProcessHelper.Entity.GetPid();

            if (null == _globlos || !_globlos.ContainsKey(pid)) return null;

            Hashtable tb = (Hashtable)_globlos[pid];

            return (null == tb || !tb.ContainsKey(key)) ? null : Validation.ToString(tb[key]);
        }

        public static bool GlobalBoolean(string key)
        {
            string v = Global(key);

            return (null == v || v.ToLower() != "n");
        }
        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void AddCache(string key, object value)
        {
            if (null == _cache)
            {
                _cache = new Hashtable();
            }

            key = string.Concat(ProcessHelper.Entity.GetPid(), "_", key);
            _cache[key] = value;
        }

        /// <summary>
        /// 根据键得到缓存中的值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetValueByKey(string key)
        {
            if (null == _cache) return null;
            key = string.Concat(ProcessHelper.Entity.GetPid(), "_", key);

            return _cache.ContainsKey(key) ? _cache[key] : null;
        }

        public static void RemoveCache(string key)
        {
            if (null == _cache) return;
            key = string.Concat(ProcessHelper.Entity.GetPid(), "_", key);

            if (_cache.ContainsKey(key))
            {
                _cache.Remove(key);
            }
        }

        /// <summary>
        /// 当前登录标识
        /// </summary>
        public static string LoginKey
        {
            get
            {
                if (null == _keys) return null;
                return Validation.ToString(_keys[ProcessHelper.Entity.GetPid()]);
            }
        }

        /// <summary>
        /// 登录名
        /// </summary>
        public static string LoginName
        {
            get
            {
                if (null == _names) return null;
                return Validation.ToString(_names[ProcessHelper.Entity.GetPid()]);
            }
        }

        /// <summary>
        /// 添加用户标识
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static bool AddUser(string guid, string name)
        {
            if (null == _keys)
            {
                _keys = new Hashtable();
                _names = new Hashtable();
            }

            if (_keys.ContainsKey(guid)) return false;

            string pid = ProcessHelper.Entity.GetPid();

            _names[pid] = name;
            _keys[pid] = guid;
            return true;
        }

        private static bool _isSuperUser = false;

        public static void AddCommonInfo(DataTable dt)
        {
            if (null == _users)
            {
                _users = new Hashtable();
            }

            string pid = ProcessHelper.Entity.GetPid();
            ContextUser user = ContextUser.Convert(dt);
            Psn0a00 psn = Psn0a00.GetPsn0a00ByDataRow(dt.Rows[0]);
            AddCache("_cominfo", psn);

            _users[pid] = user;
            _isSuperUser = (Validation.ToString(user.Supermk) == "Y"); //chaojiyonghubiaoshi 
        }

        public static bool IsSuperUser
        {
            get { return Context._isSuperUser; }
        }

        /// <summary>
        /// 得到公司信息
        /// </summary>
        public static Psn0a00 CompanyInfo
        {
            get { return GetValueByKey("_cominfo") as Psn0a00; }
        }

        /// <summary>
        /// 登出
        /// </summary>
        public static void Remove()
        {
            string pid = ProcessHelper.Entity.GetPid();
            if (null == _keys || !_keys.ContainsKey(pid)) return;

            _keys.Remove(pid);
            _users.Remove(pid);
            _names.Remove(pid);
        }

        public static ContextUser User
        {
            get
            {
                if (null == _users) return new ContextUser();
                ContextUser user = _users[ProcessHelper.Entity.GetPid()] as ContextUser;
                if (null == user) return new ContextUser();
                return user;
            }
        }

        /// <summary>
        /// 公司编码，如果公司编码为空，替换成XXXXX
        /// </summary>
        public static string Comocde
        {
            get
            {
                ContextUser user = Context.User;
                return (string.IsNullOrEmpty(user.Comcode) ? "XXXXX" : user.Comcode) + ".";
            }
        }

        public static string realComcode
        {
            get
            {
                return Context.User.Comcode;
            }
        }

        /// <summary>
        /// 公司名称
        /// </summary>
        public static string ComoName
        {
            get
            {
                ContextUser user = Context.User;
                return string.IsNullOrEmpty(user.Comname) ? "----" : user.Comname;
            }
        }

        /// <summary>
        /// 向条件中拼接公司信息，限制界面信息显示, 写在此处，方便统一管理
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static string GetAppKeyWhere(string fieldName)
        {
            return string.Format(" {0}  like '{1}.%' ", fieldName,
                string.IsNullOrEmpty(Context.User.Comcode) ? "XXXXXX" : Context.User.Comcode);
        }

        /// <summary>
        /// 向条件中拼接公司信息，限制界面信息显示, 写在此处，方便统一管理
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static string GetAppKeyWhere(string fieldName, bool addop)
        {
            return string.Format(" {0} {1}  like '{2}.%' ", (addop ? "AND" : ""),
                fieldName, string.IsNullOrEmpty(Context.User.Comcode) ? "XXXXXX" : Context.User.Comcode);
        }

        /// <summary>
        /// 向条件中拼接公司信息，限制界面信息显示, 写在此处，方便统一管理
        /// </summary>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static string GetAppKeyWhere()
        {
            return string.Format(" like '{0}.%' ", string.IsNullOrEmpty(Context.User.Comcode) ? "XXXXXX" : Context.User.Comcode);
        }

        /// <summary>
        /// 公司.部门.用户名
        /// </summary>
        public static string AppId
        {
            get
            {
                ContextUser user = Context.User;

                return (null == user ? "" : user.Comcode + ".")
                    + (null == user ? "" : user.Depcode + ".")
                    + Context.LoginName;
            }
        }

        public static void RemoveUser()
        {
            string pid = ProcessHelper.Entity.GetPid();
            if (null == _users || !_users.ContainsKey(pid)) return;

            _users.Remove(pid);
        }

        /// <summary>
        /// 检测当前用户是否已经登出了
        /// </summary>
        public static bool IsLoginOut
        {
            get
            {
                string pid = ProcessHelper.Entity.GetPid();
                return (null == _keys || !_keys.ContainsKey(pid));
            }
        }

        /// <summary>
        /// 加入登录的验证票，这个是以后的消息密钥
        /// </summary>
        /// <param name="guid"></param>
        public static void AddGuid(string guid)
        {
            if (null == _loginGuid)
            {
                _loginGuid = new Hashtable();
            }

            _loginGuid[ProcessHelper.Entity.GetPid()] = guid;
        }

        public static void RemoveLoginKey()
        {
            string pid = ProcessHelper.Entity.GetPid();
            if (null == _loginGuid || !_loginGuid.ContainsKey(pid)) return;
            _loginGuid.Remove(pid);
        }

        public static string PrivateKey
        {
            get
            {
                string pid = ProcessHelper.Entity.GetPid();
                return (null == _loginGuid || !_loginGuid.ContainsKey(pid)) ? "" : _loginGuid[pid].ToString();
            }

        }

        public static string ApplicationPath
        {
            get
            {
                return System.Windows.Forms.Application.StartupPath;
            }
        }

        static string _version;

        /// <summary>
        /// 当前程序的版本信息
        /// </summary>
        public static string Version
        {
            get { return Context._version; }
            set { Context._version = value; }
        }

        public static DateTime GetServerDate()
        {
            string t = CommonHelper.Entity.ExecFunScalar("getsysdate", null);
            return Validation.ToDate(t);
        }
    }
}
